/**
 * 调试数据结构脚本
 * 分析generateVNext返回的数据结构
 */

import { MastraClient } from '@mastra/client-js';

console.log('🔍 开始数据结构调试...\n');

const mastraClient = new MastraClient({
  baseUrl: 'http://localhost:4111',
  retries: 3,
  backoffMs: 300,
  maxBackoffMs: 5000,
});

async function debugDataStructure() {
  try {
    const agent = mastraClient.getAgent('gameMasterAgent');
    const result = await agent.generateVNext({
      messages: [
        {
          role: 'user',
          content: `请开始一个新的赛博侦探游戏。

游戏配置：
- 难度：normal
- 主题：办公室谋杀案
- 嫌疑人数量：3

请使用 game-master 工具执行 start_game 操作。`
        }
      ]
    });

    console.log('📊 完整结果结构:');
    console.log('- result.text 长度:', result.text?.length);
    console.log('- result.toolCalls 数量:', result.toolCalls?.length);
    console.log('- result.toolResults 数量:', result.toolResults?.length);

    if (result.toolCalls && result.toolCalls.length > 0) {
      console.log('\n🔧 工具调用结构:');
      const toolCall = result.toolCalls[0];
      console.log('- toolCall.type:', toolCall.type);
      console.log('- toolCall.from:', toolCall.from);
      console.log('- toolCall.payload 存在:', !!toolCall.payload);
      console.log('- toolCall.result 存在:', !!toolCall.result);
      
      if (toolCall.payload) {
        console.log('\n📦 payload 结构:');
        console.log('- payload 类型:', typeof toolCall.payload);
        console.log('- payload 键:', Object.keys(toolCall.payload));
        
        if (toolCall.payload.result) {
          console.log('\n✅ payload.result 存在:');
          console.log('- result 类型:', typeof toolCall.payload.result);
          console.log('- result 键:', Object.keys(toolCall.payload.result));
          
          const gameData = toolCall.payload.result;
          console.log('- gameData.success:', gameData.success);
          console.log('- gameData.data 存在:', !!gameData.data);
          console.log('- gameData.sessionInfo 存在:', !!gameData.sessionInfo);
          console.log('- gameData.gameState 存在:', !!gameData.gameState);
          
          if (gameData.data) {
            console.log('\n🎮 游戏数据结构:');
            console.log('- data 类型:', typeof gameData.data);
            console.log('- data 键:', Object.keys(gameData.data));
          }
        }
      }
    }

    if (result.toolResults && result.toolResults.length > 0) {
      console.log('\n🔧 工具结果结构:');
      const toolResult = result.toolResults[0];
      console.log('- toolResult.type:', toolResult.type);
      console.log('- toolResult.from:', toolResult.from);
      console.log('- toolResult.payload 存在:', !!toolResult.payload);
      
      if (toolResult.payload) {
        console.log('\n📦 toolResult payload 结构:');
        console.log('- payload 类型:', typeof toolResult.payload);
        console.log('- payload 键:', Object.keys(toolResult.payload));
        
        if (toolResult.payload.result) {
          console.log('\n✅ toolResult.payload.result 存在:');
          console.log('- result 类型:', typeof toolResult.payload.result);
          console.log('- result 键:', Object.keys(toolResult.payload.result));
          
          const gameData = toolResult.payload.result;
          console.log('- gameData.success:', gameData.success);
          console.log('- gameData.data 存在:', !!gameData.data);
          console.log('- gameData.sessionInfo 存在:', !!gameData.sessionInfo);
          console.log('- gameData.gameState 存在:', !!gameData.gameState);
          
          if (gameData.data) {
            console.log('\n🎮 游戏数据结构:');
            console.log('- data 类型:', typeof gameData.data);
            console.log('- data 键:', Object.keys(gameData.data));
            
            console.log('\n📋 案件信息:');
            if (gameData.data.case) {
              console.log('- case.title:', gameData.data.case.title);
              console.log('- case.location:', gameData.data.case.location);
              console.log('- case.victim:', gameData.data.case.victim);
            }
            
            console.log('\n👥 嫌疑人信息:');
            if (gameData.data.suspects) {
              console.log('- suspects 数量:', gameData.data.suspects.length);
              gameData.data.suspects.forEach((suspect, index) => {
                console.log(`  ${index + 1}. ${suspect.name} - ${suspect.personality}`);
              });
            }
            
            console.log('\n🔍 线索信息:');
            if (gameData.data.discoveredClues) {
              console.log('- discoveredClues 数量:', gameData.data.discoveredClues.length);
            }
          }
          
          if (gameData.sessionInfo) {
            console.log('\n📊 会话信息:');
            console.log('- sessionId:', gameData.sessionInfo.sessionId);
            console.log('- startTime:', gameData.sessionInfo.startTime);
            console.log('- difficulty:', gameData.sessionInfo.difficulty);
          }
        }
      }
    }

    return result;

  } catch (error) {
    console.error('❌ 调试失败:', error);
    return null;
  }
}

debugDataStructure().then(result => {
  if (result) {
    console.log('\n🎉 数据结构调试完成！');
  } else {
    console.log('\n❌ 数据结构调试失败！');
  }
}).catch(error => {
  console.error('❌ 调试脚本失败:', error);
});
